<!--
 * @Author: Dorange.Wang
 * @Date: 2021-03-31 16:04:51
 * @LastEditors: wangming
 * @LastEditTime: 2022-08-02 10:57:29
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>模态框全局配置</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="formData"
      inline
      ref="Form"
      label-width="120px"
      label-position="left"
    >
      <el-form-item label="弹出方式">
        <el-radio-group v-model="formData.directionType">
          <el-radio label="dialog">对话框</el-radio>
          <el-radio label="drawer">抽屉</el-radio>
        </el-radio-group>
      </el-form-item>
      <div v-if="formData.directionType === 'dialog'">
        <el-form-item label="可移动">
          <el-switch v-model="formData.dialogDrag"> </el-switch>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="点击关闭">
          <el-switch v-model="formData.closeOnModal"> </el-switch>
        </el-form-item>
      </div>
      <el-form-item label="弹框宽度">
        <el-input v-model="formData.width" style="width: 200px" isNumber>
          <el-select
            style="width: 80px"
            slot="append"
            v-model="formData.unit"
            placeholder="请选择"
            class="input-with-select"
          >
            <el-option value="px"></el-option>
            <el-option value="%"></el-option>
          </el-select>
        </el-input>
      </el-form-item>
      <el-form-item label="弹出方向" v-if="formData.directionType === 'drawer'">
        <el-radio-group v-model="formData.direction">
          <el-radio label="ltr">从左往右</el-radio>
          <el-radio label="rtl">从右往左</el-radio>
          <el-radio label="ttb">从上往下</el-radio>
          <el-radio label="btt">从下往上</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      showVisible: false,
      formData: {
        width: "",
        directionType: "",
        unit: "",
        direction: "rtl",
        dialogDrag: false,
        closeOnModal: false,
      },
      callback: () => {},
    };
  },
  methods: {
    openForm({
      width,
      directionType,
      unit,
      direction,
      closeOnModal,
      dialogDrag,
    }) {
      this.formData.dialogDrag = !!dialogDrag;
      this.formData.closeOnModal = !!closeOnModal;
      this.formData.width = width + "";
      this.formData.directionType = directionType || "dialog";
      this.formData.unit = unit || "px";
      this.formData.direction = direction || "rtl";
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        this.showVisible = false;
        this.callback(this.formData);
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-input-group__append {
  background-color: #fff;
}
</style>
